package cn.com.css.taxInspection.workflow.controller;

import cn.com.css.taxInspection.gt3.entity.*;
import cn.com.css.taxInspection.gt3.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springframework.stereotype.Controller;

import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.common.result.DataResult;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/workflow/carryout")
public class WorkflowCarryOutController {

    @Resource
    private JcAyxxService jcAyxxService;

    @Resource
    private ZxhjQstjService zxhjQstjService;

    @Resource
    private ZxhjByqlqsqktjService zxhjByqlqsqktjService;

    @Resource
    private ZxhjBlxkService zxhjBlxkService;
    @Resource
    private ZxhjQsdhService zxhjQsdhService;
    @Resource
    private ZxhjQqtjService zxhjQqtjService;

    @Resource
    private ZxhjZxtzService zxhjZxtzService;
    @ApiOperation(value = "案源情况说明-列表")
    @LogAnnotation(title = "案源情况说明", action = "列表")
    @PostMapping("/list")
    @RequiresPermissions("workflow:carryout:list")
    @ResponseBody
    public DataResult list(@RequestBody JcAyxxEntity jcAyxxEntity) {
        IPage<JcAyxxEntity> iPage = jcAyxxService.pageInfo(jcAyxxEntity);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "执行台账-稽查欠税统计表")
    @LogAnnotation(title = "执行台账", action = "执行台账-稽查欠税统计表")
    @PostMapping("/queryZxtzQstjTable")
    @ResponseBody
    public DataResult queryZxtzQstjTable(@RequestBody ZxhjQstjEntity zxhjQstjEntity) throws ParseException {
        IPage<ZxhjQstjEntity> iPage = zxhjQstjService.queryZxtzQstjTable(zxhjQstjEntity);
        return DataResult.success(iPage);
    }

//    @ApiOperation(value = "执行台账-稽查欠税统计表-导出")
//    @LogAnnotation(title = "执行台账", action = "执行台账-稽查欠税统计表-导出")
//    @GetMapping("/exportZxtzQstjTable")
//    public void exportZxtzQstjTable(@RequestBody ZxhjQstjEntity zxhjQstjEntity) throws ParseException {
//        List<ZxhjQstjDcDto> zxhjQstjDcDtos = zxhjQstjService.queryZxtzQstjDcTable(zxhjQstjEntity);
//        return DataResult.success(iPage);
//    }

    @ApiOperation(value = "执行台账")
    @LogAnnotation(title = "执行台账", action = "执行台账")
    @PostMapping("/queryZxtzTable")
    @ResponseBody
    public DataResult queryZxtzTable(@RequestBody ZxhjZxtzEntity zxhjZxtzEntity) throws ParseException {
        IPage<ZxhjZxtzEntity> iPage = zxhjZxtzService.queryZxtzTable(zxhjZxtzEntity);
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "执行台账-欠税大户台账")
    @LogAnnotation(title = "执行台账", action = "执行台账-欠税大户台账")
    @PostMapping("/queryZxtzQsdhTable")
    @ResponseBody
    public DataResult queryZxtzQsdhTable(@RequestBody ZxhjQsdhEntity zxhjQsdhEntity) throws ParseException {
        IPage<ZxhjQsdhEntity> iPage = zxhjQsdhService.queryZxtzQsdhTable(zxhjQsdhEntity);
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "欠税台账-新增欠税统计人工录入")
    @LogAnnotation(title = "欠税台账", action = "新增欠税统计人工录入")
    @PostMapping("/insertZxtzQstjRglr")
    @ResponseBody
    public DataResult insertZxtzQstjRglr(@RequestBody ZxhjQstjEntity zxhjQstjEntity) throws ParseException {
        Boolean relust = zxhjQstjService.insertZxtzQstjRglr(zxhjQstjEntity);
        return DataResult.success(relust);
    }

    @ApiOperation(value = "欠税台账-新增欠税大户人工录入")
    @LogAnnotation(title = "欠税台账", action = "新增欠税大户人工录入")
    @PostMapping("/insertZxtzQsdhRglr")
    @ResponseBody
    public DataResult insertZxtzQsdhRglr(@RequestBody ZxhjQsdhEntity zxhjQsdhEntity)  {
        Boolean relust = zxhjQsdhService.insertZxtzQsdhRglr(zxhjQsdhEntity);
        return DataResult.success(relust);
    }


//    @ApiOperation(value = "执行台账-新增执行台账人工录入")
//    @LogAnnotation(title = "执行台账", action = "新增执行台账人工录入")
//    @PostMapping("/insertZxtzRglr")
//    @ResponseBody
//    public DataResult insertZxtzRglr(@RequestBody ZxhjZxtzEntity zxhjZxtzEntity)  {
//        Boolean relust = zxhjZxtzService.insertZxtzRglr(zxhjZxtzEntity);
//        return DataResult.success(relust);
//    }

    @ApiOperation(value = "执行台账-本月清理欠税情况统计表")
    @LogAnnotation(title = "执行台账", action = "执行台账-本月清理欠税情况统计表")
    @PostMapping("/queryByqlqsqktjTable")
    @ResponseBody
    public DataResult queryByqlqsqktjTable(@RequestBody ZxhjByqlqsqktjEntity zxhjByqlqsqktjEntity) throws ParseException {
        IPage<ZxhjByqlqsqktjEntity> iPage = zxhjByqlqsqktjService.queryZxhjByqlqsqktjTable(zxhjByqlqsqktjEntity);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "欠税台账-本月清理欠税情况统计表")
    @LogAnnotation(title = "欠税台账", action = "本月清理欠税情况统计表人工录入")
    @PostMapping("/insertZxtzByqlqqqktjbRglr")
    @ResponseBody
    public DataResult insertZxtzByqlqqqktjbRglr(@RequestBody ZxhjByqlqsqktjEntity zxhjByqlqsqktjEntity) {
        Boolean relust = zxhjByqlqsqktjService.insertZxtzByqlqqqktjbRglr(zxhjByqlqsqktjEntity);
        return DataResult.success(relust);
    }

    @ApiOperation(value = "执行台账-更新清欠统计表备注")
    @LogAnnotation(title = "执行台账", action = "更新清欠统计表备注")
    @PostMapping("/updateZxtzQqtjbz")
    @ResponseBody
    public DataResult updateZxtzQqtjbz(@RequestBody ZxhjQqtjEntity zxhjQqtjEntity) {
        Boolean relust = zxhjQqtjService.updateQqtjbz(zxhjQqtjEntity);
        return DataResult.success(relust);
    }

    @ApiOperation(value = "执行台账-暴力虚开")
    @LogAnnotation(title = "查询", action = "执行台账-暴力虚开")
    @PostMapping("/queryBlxkTable")
    @ResponseBody
    public DataResult queryBlxkTable(@RequestBody ZxhjBlxkEntity zxhjBlxkEntity, @RequestParam(defaultValue = "1") int pageNum,
                                     @RequestParam(defaultValue = "10") int pageSize) throws ParseException {
        List<ZxhjBlxkEntity> resultList  = zxhjBlxkService.queryBlxkTable(zxhjBlxkEntity);
        // 转换为分页对象
        IPage<ZxhjBlxkEntity> page = convertListToPage(resultList, pageNum, pageSize);
        return DataResult.success(page);

    }


    @ApiOperation(value = "执行台账-统计表")
    @LogAnnotation(title = "查询", action = "执行台账-本月清理欠税情况统计表")
    @PostMapping("/queryQqtjTable")
    @ResponseBody
    public DataResult queryQqtjTable(@RequestBody ZxhjQqtjEntity zxhjQqtjEntity, @RequestParam(defaultValue = "1") int pageNum,
                                     @RequestParam(defaultValue = "12") int pageSize) throws ParseException {
        List<ZxhjQqtjEntity> resultList  = zxhjQqtjService.queryQqtjTable(zxhjQqtjEntity);
        // 转换为分页对象
        IPage<ZxhjQqtjEntity> page = convertQqtjListToPage(resultList, pageNum, pageSize);
        return DataResult.success(page);

    }

    /**
     * 将List转换为分页对象
     */
    private IPage<ZxhjBlxkEntity> convertListToPage(List<ZxhjBlxkEntity> list, int pageNum, int pageSize) {
        Page<ZxhjBlxkEntity> page = new Page<>(pageNum, pageSize);
        // 计算分页数据
        int total = list.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);
        // 设置分页属性
        page.setRecords(list.subList(fromIndex, toIndex));
        page.setTotal(total);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    /**
     * 将List转换为分页对象
     */
    private IPage<ZxhjQqtjEntity> convertQqtjListToPage(List<ZxhjQqtjEntity> list, int pageNum, int pageSize) {
        Page<ZxhjQqtjEntity> page = new Page<>(pageNum, pageSize);
        // 计算分页数据
        int total = list.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);
        // 设置分页属性
        page.setRecords(list.subList(fromIndex, toIndex));
        page.setTotal(total);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

}
